<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>fangyin来到王者峡谷</title>
<%@include file="/commonPage/heard.jsp" %>

<style type="text/css">
*{margin: 5px 15px 5px 5px;}
</style>

<script type="text/javascript">

$(function(){
	displayMeun('ul1');
	getRole();
	$("input[type='checkbox']").click(function(){
		var val=$(this).prop("checked");
		if(val){
			if(confirm("确定要添加当前用户角色？")){
				addRole($(this).val());
			}
		}else{
			if(confirm("确定要删除当前用户角色？")){
				delRole($(this).val());
			}
		}
	});
});

function getRole(){
	$(".content").show();
	$.ajax({
		url: "UserServlet",
		data:{"_method":"role"},
        dataType: "json",
        type: "post",
        async: false,
        success: function (result, status) {
        	console.log(result);
        	var body="";
        	$(result.allRole).each(function () {
        		var haveID=this.id;
        		var checked=0;
        		$(result.userRole).each(function () {
        			if(haveID==this.id){
        				checked=1;
        				return;
        			}
        		});
        		if(checked==1){
        			body+="<li><label><input type='checkbox' checked='checked'"
        			+" name='roleName' value='"+this.id+"' id='role"+this.id+"'/>"
        			+this.roleName+"</label></li>";
        		}else{
        			body+="<li><label><input type='checkbox' name='roleName'"
        			+" value='"+this.id+"' id='role"+this.id+"'/>"
        			+this.roleName+"</label></li>";
        		}
        		
        	});
        	$("#content").html("");
        	$(body).appendTo("#content");
        }
	});
}

function addRole(val){
	$.ajax({
		url: "UserServlet",
		data:{"_method":"addrole","roleid":val},
        dataType: "json",
        type: "post",
        async: false,
        success: function (result, status) {
        	if(result.flag==1){
        		alert("添加角色成功");
        	}else{
        		alert("系统或数据异常");
        	}
        }
	});
}
function delRole(val){
	$.ajax({
		url: "UserServlet",
		data:{"_method":"delRole","roleid":val},
        dataType: "json",
        type: "post",
        async: false,
        success: function (result, status) {
        	if(result.flag==1){
        		alert("删除角色成功");
        	}else{
        		alert("系统或数据异常");
        	}
        }
	});
}
</script>
</head>
<body>
	<div id="popLayer"></div>
	<div id="popBox" >
		<div class="close">
				<a href="javascript:void(0)" onclick="closeBox()">X</a>
		</div>
		<div  style="text-align:center;margin-top: 50px;">
			<div class="input-group">
            	<span class="input-group-addon">用户名</span>
            	<input class="form-control" type="text" id="username" name="username"/><br>
        	</div>
        	<div class="input-group">
            	<span class="input-group-addon">密码</span>
            	<input class="form-control" type="password" id="pwd" name="password"/><br>
        	</div>
			
					<input type="hidden" id="flag" name="flag"/>
					<button onclick="saveUser()">保存</button>
		</div>
	</div>
	<h2>${user.name},您好。当前在线总人数为:${onLineCount}<label id="time"></label></h2>
	<hr>
	<div class="menu">
		<%@include file="/commonPage/menu.jsp" %>
	</div>
	
	<div class="content">
		<ul id = "content"></ul>
	</div>
	<input type="hidden" id="loginerId" value="${user.id}"/>
	<input type="hidden" id="loginerPwd" value="${user.phone}"/>
	<input type="hidden" id="loginerName" value="${user.name}"/>
	<input type="hidden" id="loginerDeptId" value="${user.deptId}"/>
</body>
</html>